package com.cwj;

import java.util.Arrays;
import java.util.Optional;
import java.util.TreeMap;

/**
 * 离散化数组
 */
public class Discretization {
    private final TreeMap<Integer, Integer> map;

    public Discretization(int[] a) {
        this.map = new TreeMap<>();
        Arrays.sort(a);
        for (int i = 0, cur = 1; i < a.length; i++) {
            if (map.containsKey(a[i])) {
                continue;
            }
            map.put(a[i], cur++);
        }
    }

    public int query(int i) {
        return Optional.ofNullable(map.get(i)).orElseThrow(IllegalArgumentException::new);
    }
}
